package com.dongdong.bookstore.security.service;

import com.dongdong.bookstore.data.ResourceData;
import com.dongdong.bookstore.module.ums.model.Resource;
import com.dongdong.bookstore.module.ums.service.ResourceService;
import com.dongdong.bookstore.module.ums.service.UmsCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chiangkai
 * @date 2021-09-21
 * @email chiangkk@163.com
 */
@Service
public class DynamicSecurityServiceImpl implements DynamicSecurityService {

    @Autowired
    private UmsCacheService umsCacheService;
    
    @Override
    public Map<String, ConfigAttribute> loadDataSource() {
        Map<String, ConfigAttribute> map = new ConcurrentHashMap<>();
        List<Resource> resourceList = umsCacheService.getResourceListAll();
        /*静态数据  ResourceData.getAll(); */
        for (Resource resource : resourceList) {
            map.put(resource.getUrl(), new org.springframework.security.access.SecurityConfig(resource.getId() + ":" + resource.getName()));
        }
        return map;
    }
}
